﻿using UnityEngine;
using System.Collections;


public class lqf_move : MonoBehaviour {
    public delegate void EventMove(bool e1,bool e2,bool e3,bool e4);
    public event EventMove AI_Move;
    private Animator animator;
    private AudioSource source;
    private Rigidbody2D body2d;
	// Use this for initialization


	void Start () {
        animator = GetComponent<Animator>();
        source = GetComponent<AudioSource>();
        body2d = GetComponent<Rigidbody2D>();
	}
	
	// Update is called once per frame
	void Update (){
        
        AnimatorStateInfo StateInfo = animator.GetCurrentAnimatorStateInfo(0);

     
        if (StateInfo.IsName("Base Layer.lqf_idea"))
        {
            if (Input.GetKey(KeyCode.W))
            {
                source.Play();
                animator.SetBool("top", true);
            }

            if (Input.GetKey(KeyCode.S))
            {
                source.Play();
                animator.SetBool("bottom", true);
            }

            if (Input.GetKey(KeyCode.A))
            {
                source.Play();
                animator.SetBool("left", true);
            }


            if (Input.GetKey(KeyCode.D))
            {
                source.Play();
                animator.SetBool("right", true);
            }

        }
	
    //-----------------
        if (StateInfo.IsName("Base Layer.lqf_top"))
        {
            
            animator.SetBool("top", false);           
            if (Input.GetKeyDown(KeyCode.W))
            {
                source.Play();
                animator.SetBool("top", true);
                animator.Play("lqf_top", 0, 0.0f);
                
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                source.Play();
                animator.SetBool("bottom", true);
                animator.Play("lqf_bottom", 0, 0.0f);
              //  source.Stop();
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                source.Play();
                animator.SetBool("left", true);
                animator.Play("lqf_left", 0, 0.0f);
               // source.Stop();
            }


            if (Input.GetKeyDown(KeyCode.D))
            {
                source.Play();
                animator.SetBool("right", true);
                animator.Play("lqf_right", 0, 0.0f);
               // source.Stop();
            }

        }
    //------------
        if (StateInfo.IsName("Base Layer.lqf_left"))
        {
           
            animator.SetBool("left", false);
            if (Input.GetKeyDown(KeyCode.W))
            {
                source.Play();
                animator.SetBool("top", true);
                animator.Play("lqf_top", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                source.Play();
                animator.SetBool("bottom", true);
                animator.Play("lqf_bottom", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                source.Play();
                animator.SetBool("left", true);
                animator.Play("lqf_left", 0, 0.0f);
            }


            if (Input.GetKeyDown(KeyCode.D))
            {
                source.Play();
                animator.SetBool("right", true);
                animator.Play("lqf_right", 0, 0.0f);
            }

        }

        //---------
        if (StateInfo.IsName("Base Layer.lqf_bottom"))
        {

            animator.SetBool("bottom", false);
            if (Input.GetKeyDown(KeyCode.W))
            {
                source.Play();
                animator.SetBool("top", true);
                animator.Play("lqf_top", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                source.Play();
                animator.SetBool("bottom", true);
                animator.Play("lqf_bottom", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                source.Play();
                animator.SetBool("left", true);
                animator.Play("lqf_left", 0, 0.0f);
            }


            if (Input.GetKeyDown(KeyCode.D))
            {
                source.Play();
                animator.SetBool("right", true);
                animator.Play("lqf_right", 0, 0.0f);
            }
        }
        //---------
        if (StateInfo.IsName("Base Layer.lqf_right"))
        {
            animator.SetBool("right", false);
            if (Input.GetKeyDown(KeyCode.W))
            {
                source.Play();
                animator.SetBool("top", true);
                animator.Play("lqf_top", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                source.Play();
                animator.SetBool("bottom", true);
                animator.Play("lqf_bottom", 0, 0.0f);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                source.Play();
                animator.SetBool("left", true);
                animator.Play("lqf_left", 0, 0.0f);
            }


            if (Input.GetKeyDown(KeyCode.D))
            {
                source.Play();
                animator.SetBool("right", true);
                animator.Play("lqf_right", 0, 0.0f);
            }

        }

    }


    void OnAnimatorMove()
    {
        AnimatorStateInfo StateInfo = animator.GetCurrentAnimatorStateInfo(0);



        if (animator.GetBool("top") && !StateInfo.IsName("Base Layer.lqf_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 16f);
            transform.position = v2;

        }


        if (animator.GetBool("bottom") && !StateInfo.IsName("Base Layer.lqf_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 16f);
            transform.position = v2;
        }






        if (animator.GetBool("left") && !StateInfo.IsName("Base Layer.lqf_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x - 16f, gameObject.transform.position.y);
            transform.position = v2;
        }


        if (animator.GetBool("right") && !StateInfo.IsName("Base Layer.lqf_idea"))
        {
            Vector2 v2 = new Vector2(gameObject.transform.position.x + 16f, gameObject.transform.position.y);
            transform.position = v2;
        }

        

    }

    void OnCollisionEnter2D(Collision2D col)
    {
       

        bool bool_top = animator.GetBool("top");
        bool bool_left = animator.GetBool("left");
        bool bool_bottom = animator.GetBool("bottom");
        bool bool_right = animator.GetBool("right");
        if (AI_Move != null)
        {
            AI_Move(bool_top,bool_left,bool_bottom,bool_right);
        }
    }


}
